package string;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class TestDemo {
    public static void main(String[] args) {
        String str="name=zhansan&age=18";
        String[] strs=str.split("&");
        for (String ss: strs){
            String[] strs2=ss.split("=");
            for(String s:strs2){
                System.out.println(s);
            }
        }
    }
    public static void main11(String[] args) {
        String str="how are you";
        String[] strs=str.split(" ");//以空格进行拆分
        System.out.println(Arrays.toString(strs));//[how, are, you]
        String[] strs2=str.split(" ",2);//以空格分割，最多分成两组
        System.out.println(Arrays.toString(strs2));//[how, are you]
        String str2="192.168.0.1";
        String[] strs3=str2.split("\\.");//特殊符号是需要转义的
        System.out.println(Arrays.toString(strs3));//[192, 168, 0, 1]
        String str3="192\\168\\1\\1";
        String[] strs4=str3.split("\\\\");
        System.out.println(Arrays.toString(strs4));//[192, 168, 1, 1]
        String str4="a@b c#d";
        String[] strs5=str4.split("@| |#");//以多个字符分割
        System.out.println(Arrays.toString(strs5));//[a, b, c, d]
    }
    public static void main10(String[] args) {
        String str="abcdabbaccda";
        //replace既可以替换字符串又可以替换字符
        String str2 = str.replace('a', 'A');//将所有字符a替换成A
        System.out.println(str2);//AbcdAbbAccdA
        String str3=str.replace("ab","A");//将所有字符串ab替换成字符串A
        System.out.println(str3);//AcdAbaccda

        String str4=str.replaceAll("ab","AB");//将所有字符串ab替换成字符串AB
        System.out.println(str4);//ABcdABbaccda

        String str5=str.replaceFirst("ab","AB");//将第一个ab替换成AB
        System.out.println(str5);//ABcdabbaccda
    }
    public static void main9(String[] args) {
        String str1="abcd";
        boolean flg=str1.contains("abc");//是否包含字符串
        System.out.println(flg);//true

        String str2="abadabcdabcc";
        int index=str2.indexOf("abc");//从头查找字符串首次出现的位置
        System.out.println(index);//4
        int index2=str2.indexOf("abc",5);//从给定位置开始查找
        System.out.println(index2);//8
        int index3=str2.lastIndexOf("abc");//从最后一个位置往前查找字符串首次出现的位置
        System.out.println(index3);//8
        int index4=str2.lastIndexOf("abc",7);//给定位置从后往前查找字符串首次出现的位置
        System.out.println(index4);//4

        String str3="ababaabcdefab";
        boolean flg2 = str3.startsWith("ab");//是否以某个字符串开头开头
        System.out.println(flg2);//true
        boolean flg3=str3.startsWith("aba",5);//从指定位置开始是否以某个字符串开头
        System.out.println(flg3);//false
        boolean flg4=str3.endsWith("ab");//是否以某个字符串结尾
        System.out.println(flg4);//true
    }
    public static void main8(String[] args) {
        String str1="hello";
        String str2="Hello";
        System.out.println(str1.equalsIgnoreCase(str2));//true

        String str3="前";
        String str4="后";
        System.out.println(str3.compareTo(str4));//-449
    }
    public static void main7(String[] args) throws UnsupportedEncodingException {
        byte[] bytes= new byte[]{97, 98, 99, 100};
        String str=new String(bytes);
        System.out.println(str);//abcd

        String str2=new String(bytes,1,3);//offset:偏移量，length:取的长度
        System.out.println(str2);//bcd

        byte[] bytes1=str.getBytes();//将字符串转为字节数组
        System.out.println(Arrays.toString(bytes1));//[97, 98, 99, 100]

        String str3="芊芊子";
        byte[] bytes2=str3.getBytes("GBK");//字符集编码
        System.out.println(bytes2);//[-24, -118, -118, -24, -118, -118, -27, -83, -112]
    }
    public static void main6(String[] args) {
        char[] chs={'h','e','l','l','o'};
        String str=new String(chs);
        System.out.println(str);//hello
        String str2=new String(chs,1,3);//offset:偏移量，count:截取个数
        System.out.println(str2);//ell

        char ch=str.charAt(1);//获取字符串str下标为1的字符
        System.out.println(ch);//e

        char[] chs2=str.toCharArray();//将字符串转为字符数组
        System.out.println(Arrays.toString(chs2));//[h, e, l, l, o]
    }
    public static void main5(String[] args) {
        String str = "hello" ;
        str = str + " world" ;
        str += "!!!" ;
        System.out.println(str);//hello world!!!
    }
    public static void main4(String[] args) {
        String str1="hello";
        String str2=new String("hello").intern();//手动入池
        System.out.println(str1==str2);//true
    }
    public static void main3(String[] args) {
        String str1="hello";
        String str2=str1;
        System.out.println(str1);//hello
        System.out.println(str2);//hello
        System.out.println("=================");
        str1="abc";
        System.out.println(str1);//abc
        System.out.println(str2);//hello
    }
    public static void main2(String[] args) {
        String str1 = "hello";
        String str2 = new String("hello");

        System.out.println(str1==str2);//false

        String str3="he"+"llo";//在编译的时候就会直接拼接成 "hello"
        System.out.println(str1==str3);//true

        String str4="he";
        String str5="llo";
        String str6=str4+str5;
        System.out.println(str1==str6);//false

        String str7="he"+new String("llo");
        System.out.println(str1==str7);//false
    }
    public static void main1(String[] args) {
        // 方式一
        String str1 = "Hello";
        // 方式二
        String str2 = new String("Hello");
        // 方式三
        char[] array = {'a', 'b', 'c'};
        String str3 = new String(array);
        System.out.println(str1.equals(str2));//true
    }
}
